import cv2
#加载分类器
mask_detector = cv2.CascadeClassifier(r'E:\pycharmWorkspace\trainTest\mask\xml\cascade.xml')
img = cv2.imread(r'D:\0001.jpg')
#转成灰度图片
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
#进行预测
mask_face = mask_detector.detectMultiScale(gray, 1.1, 5, cv2.CASCADE_SCALE_IMAGE, (50,50), (200, 200))
for (x2, y2, w2, h2) in mask_face:
    cv2.rectangle(img, (x2, y2), (x2 + w2, y2 + h2), (0, 255, 0), 2)
    cv2.putText(img, "have_mask", (x2, y2), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
    cv2.imshow('mask', img)
cv2.imshow('mask', img)
cv2.imwrite(r'D:/test.jpg', img)
cv2.waitKey()